using UnityEngine;
using Yoozoo.Framework.Manager.ResourceSystem.Runtime;

namespace com.yoozoo.gta.Tools.UI
{
    public class SpriteRendererRadar : MonoBehaviour,IWebImageHandler
    {
        private WebImageHandler _webImageHandler;
        private string m_spriteName;
        public SpriteRenderer _spriteRenderer;

        public string spriteName
        {
            get { return m_spriteName; }
            set
            {
                if (m_spriteName == value)
                    return;
                if (WebImageManager.IsFromWeb(this.m_spriteName))
                    WebImageManager.Instance.RemoveLoadWebImageHandler(this, this.m_spriteName, _webImageHandler);
                
                m_spriteName = value;
                
                if (WebImageManager.IsFromWeb(value))
                {
                    if (_webImageHandler == null) _webImageHandler = new WebImageHandler(this);
                    WebImageManager.Instance.AddLoadWebImageHandler(this, value, _webImageHandler);
                }
            }
        }
        
        public void LoadWebImageSuccess(Texture2D texture, string imageUrl)
        {
            if (this.spriteName == imageUrl && texture != null)
            {
                this._spriteRenderer.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
                    new Vector2(0.5f, 0.5f));
                // _successDelegate?.Invoke(imageUrl);
            }
        }
        
        public void LoadWebImageFail(string imageUrl)
        {
            // _failDelegate?.Invoke(imageUrl);
        }
        
    }
}